package api

import (
	"book-hub/api-getaway/basic/config"
	"book-hub/api-getaway/basic/handler/request"
	__ "book-hub/api-getaway/basic/proto"
	"book-hub/book-service/basic/pkg"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
)

func UserLogin(c *gin.Context) {

	var user request.User
	err := c.ShouldBind(&user)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 0,
			"msg":  "参数错误",
		})
		return
	}
	r, err := config.BookClient.UserLogin(c, &__.UserLoginReq{
		Username: user.Username,
		Password: user.Password,
	})
	if err != nil {
		log.Fatalf("could not greet: %v", err)
	}
	handler, err := pkg.TokenHandler(user.ID)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 0,
			"msg":  "生成失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": r.Code,
		"msg":  handler,
	})
}
